package com.itheima.Service.Impl;

import com.itheima.Service.OrderSettingService;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     * @return
     */
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        orderSettingMapper.editNumberByOrderDate(orderSetting);
    }

    /**
     * 根据年月查询对应的预约设置信息
     * @param month
     * @return
     */
    public List<OrderSetting> getOrderSettingByMonth(String month) {
       List<OrderSetting> orderSettings =  orderSettingMapper.getOrderSettingByMonth(month);
       return orderSettings;
    }

    /**
     * 文件上传
     * @param list
     * @throws Exception
     */
    public void add(List<OrderSetting> list) {
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //检查此数据（日期）是否存在
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if(count > 0){
                    //已经存在，执行更新操作
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }else{
                    //不存在，执行添加操作
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }
}
